package weWork;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class WeWork{

    private WebDriver driver;

    void start() throws IOException, InterruptedException {
        //todo: 多浏览器兼容性时要根据变量调整
        driver = new ChromeDriver();
        loadCookie();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(6));

    }

    public MainPage defaultPage() throws IOException, InterruptedException {
        start();
        return new MainPage(driver);

    }

    void loadCookie() throws IOException,InterruptedException{

        ObjectMapper mapper=new ObjectMapper(new YAMLFactory());
        com.fasterxml.jackson.core.type.TypeReference typeReference=new com.fasterxml.jackson.core.type.TypeReference<List<HashMap<String,Object>>>(){};
        List<HashMap<String,Object>>cookies=null;
        try{
            cookies=mapper.readValue(this.getClass().getResource("cookie.yaml"),typeReference);
        } catch (IOException e){
            e.printStackTrace();
            }
        driver.get("https://work.weixin.qq.com/wework_admin/frame");
        cookies.forEach(cookieMap->{
            driver.manage().addCookie(new Cookie(cookieMap.get("name").toString(),cookieMap.get("value").toString()));
        });
        driver.navigate().refresh();
    }
}
